﻿using project.B12.IRepository.Base;
using project.B12.IRepository;
using project.B12.Model.Models;
using project.B12.Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.Xml.Linq;

namespace project.B12.Repository.Repository
{
    public class MedicineRepository : SugarRepository<Table_medicine>, IMedicineRepository
    {

        private IUnitOfWork UOW;
        public MedicineRepository(IUnitOfWork pUOW) : base(pUOW)
        {
            UOW = pUOW;
        }


        /// <summary>
        /// 根据药简称查询结果
        /// </summary>
        /// <returns></returns>
        public List<Table_medicine> QueryByAbbreviation(string abbrev)
        {
            List<Table_medicine> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.Abbreviation == abbrev && x.IsDelete == false).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据中文名查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Table_medicine QueryByCNName(string name)
        {
            Table_medicine result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.CN_Name == name && x.IsDelete == false).Result.FirstOrDefault();
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public List<Table_medicine> QueryAllMedicine()
        {
            List<Table_medicine> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.IsDelete == false).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public void UpdateMedicine(Table_medicine medicene)
        {
            try
            {
                UOW.BeginTran();
                Update(medicene);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        public void AddMedicine(Table_medicine medicene)
        {
            try
            {
                UOW.BeginTran();
                int i=Add(medicene).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        public int QueryIdByName(string name)
        {
            int id=0;
            try
            {
                UOW.BeginTran();
                Table_medicine  result = Query(x => x.IsDelete == false&&x.CN_Name==name).Result.First();
                id= result.Id;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                return 0;
            }
            return id;
        }

        public Table_medicine QueryById(int id)
        {
            Table_medicine medicine;
            try
            {
                UOW.BeginTran();
                medicine = Query(x => x.IsDelete == false && x.Id == id).Result.First();
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                return null;
            }
            return medicine;
        }
    }
}
